#include "process_bar.h"

const char* ch = "|/-\\";

// v1
/*
void process_bar()
{
   // printf("hello linux!\n");
    int cnt = 0;
    char bar[NUMS];
    int len = strlen(ch);
    memset(bar, '\0', sizeof(bar));
    while(cnt <= 100)
    {
        printf("[%-100s][%-2d%%][%c]\r", bar, cnt, ch[cnt%4]); //此处要用到回车符，因为显示器默认是换行才刷新屏幕，所以我们需要手动刷新屏幕
        fflush(stdout); //刷新屏幕
        usleep(100000);
        bar[cnt++] = STYLE;
        if(cnt < 100)
            bar[cnt] = ARROW;
    }
    printf("\n");
}
*/

// v2

char bar[NUMS];

void init_bar()
{
    memset(bar, '\0', sizeof(bar));
}

void process_bar(int rate)
{
    if(rate < 0 || rate > 100)
        return;
    int len = strlen(ch);
    printf(YELLOW"[%-100s]"NONE"[%d%%][%c]\r", bar, rate, ch[rate%len]);
    fflush(stdout);
    bar[rate++] = STYLE;
    if(rate < 100)
        bar[rate] = ARROW;

}
